/**
 * Copyright (C) 2025 maminjie <canpool@163.com>
 * SPDX-License-Identifier: Apache-2.0
 */

/* includes (local) ----------------------------------------------------------*/
/* includes (standard library, system) ---------------------------------------*/
#include <time.h>
#include <stdio.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include "lvsf.h"
#include "gui_app_fwk.h"
#include "lv_ext_resource_manager.h"

/* defines -------------------------------------------------------------------*/
#define APP_ID "calendar"

#define CALENDAR_HEADER_H        70
#define CALENDAR_HEDAER_PAD_TOP  15
#define CALENDAR_HEDAER_PAD_LEFT 40
#define CALENDAR_HEDAER_PAD_GAP  5
#define CALENDAR_HEDAER_BUTTON_W 48
#define CALENDAR_HEDAER_BUTTON_H 48
#define CALENDAR_BODY_PAD_TOP    5
#define CALENDAR_BODY_PAD_BOTTOM 40

/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
LV_IMG_DECLARE(img_calendar);
LV_IMG_DECLARE(img_left_arrow);
LV_IMG_DECLARE(img_right_arrow);
/* variables (local) ---------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

static void on_start(void)
{
    lv_coord_t scr_hor_res, scr_ver_res;
    scr_hor_res = lv_disp_get_hor_res(lv_disp_get_default());
    scr_ver_res = lv_disp_get_ver_res(lv_disp_get_default());

    lv_obj_t *calendar = lv_calendar_create(lv_scr_act());
    lv_obj_set_size(calendar, scr_hor_res, scr_ver_res);
    lv_obj_align(calendar, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *body = lv_obj_get_child(calendar, 0);
    if (body) {
        lv_obj_set_style_pad_top(body, CALENDAR_BODY_PAD_TOP, LV_PART_MAIN);
        lv_obj_set_style_pad_bottom(body, CALENDAR_BODY_PAD_BOTTOM, LV_PART_MAIN);
        lv_obj_update_layout(body);
    }

    time_t now = time(RT_NULL);
    struct tm *local_time = localtime(&now);
    int year = local_time->tm_year + 1900;
    int month = local_time->tm_mon + 1;
    int day = local_time->tm_mday;
    int hour = local_time->tm_hour;
    int minute = local_time->tm_min;
    int second = local_time->tm_sec;
    printf("current time: %04d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second);
    lv_calendar_set_today_date(calendar, year, month, day);
    lv_calendar_set_showed_date(calendar, year, month);

#if LV_USE_CALENDAR_HEADER_ARROW
    lv_obj_t *header = lv_calendar_header_arrow_create(calendar);

    lv_obj_set_height(header, CALENDAR_HEADER_H);
    lv_obj_set_style_pad_left(header, CALENDAR_HEDAER_PAD_LEFT, LV_PART_MAIN);
    lv_obj_set_style_pad_top(header, CALENDAR_HEDAER_PAD_TOP, LV_PART_MAIN);
    lv_obj_set_style_pad_right(header, CALENDAR_HEDAER_PAD_LEFT, LV_PART_MAIN);
    lv_obj_set_style_pad_bottom(header, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_gap(header, CALENDAR_HEDAER_PAD_GAP, LV_PART_MAIN);
    lv_obj_update_layout(header);

    lv_obj_t *mo_prev = lv_obj_get_child(header, 0);
    lv_obj_t *mo_next = lv_obj_get_child(header, 2);
    lv_obj_set_size(mo_prev, CALENDAR_HEDAER_BUTTON_W, CALENDAR_HEDAER_BUTTON_H);
    lv_obj_set_size(mo_next, CALENDAR_HEDAER_BUTTON_W, CALENDAR_HEDAER_BUTTON_H);
#ifndef BSP_USING_PC_SIMULATOR
    // FIXME: Using lv_obj_set_style_bg_img_src to set the icon for the button cannot be displayed
    // on the board. If LV_SYMBOL_LEFT or LV_SYMBOL_RIGHT is used as the icon for the img,
    // it cannot be displayed normally on the board (It might be caused by the font)
    // WORKAROUND: Embed an img object within the button, and then set an external image for the
    // img object.
    lv_obj_t *icon = lv_img_create(mo_prev);
    lv_img_set_src(icon, LV_EXT_IMG_GET(img_left_arrow));
    lv_obj_center(icon);
    icon = lv_img_create(mo_next);
    lv_img_set_src(icon, LV_EXT_IMG_GET(img_right_arrow));
    lv_obj_center(icon);
#else
    // The Chinese font causes LV_SYMBOL_LEFT and LV_SYMBOL_RIGHT to only display a white box.
    // If the solution of embedding an img object within the button is adopted, the white box
    // will still be displayed
    lv_obj_set_style_bg_img_src(mo_prev, LV_EXT_IMG_GET(img_left_arrow), 0);
    lv_obj_set_style_bg_img_src(mo_next, LV_EXT_IMG_GET(img_right_arrow), 0);
#endif
    lv_obj_update_layout(mo_prev);
    lv_obj_update_layout(mo_next);
#endif
}

static void on_resume(void)
{
}

static void on_pause(void)
{
}

static void on_stop(void)
{
}

static void msg_handler(gui_app_msg_type_t msg, void *param)
{
    switch (msg) {
    case GUI_APP_MSG_ONSTART:
        on_start();
        break;

    case GUI_APP_MSG_ONRESUME:
        on_resume();
        break;

    case GUI_APP_MSG_ONPAUSE:
        on_pause();
        break;

    case GUI_APP_MSG_ONSTOP:
        on_stop();
        break;
    default:
        break;
    }
}

static int app_main(intent_t i)
{
    gui_app_regist_msg_handler(APP_ID, msg_handler);

    return 0;
}

BUILTIN_APP_EXPORT(LV_EXT_STR_ID(calendar), LV_EXT_IMG_GET(img_calendar), APP_ID, app_main);
